
<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/**
 * View file: index
 *
 * @author hoanguyen
 * @since 13/9/2013
 */
?>
<input type="hidden" name="base_url" id="base_url" value="<?php echo base_url() ?>" />

<div id="result"> </div>
<?php if (isset($del_noti)): ?>
    <h4 class="alert_success"><?php echo $del_noti ?></h4>
<?php endif; ?>

<article class="module width_full">
    <header>
        <div style="width: 50%; float: left">
            <h3>
                <?php echo $title ?>
                <span id="total_movies"></span>
                <img src="<?php echo base_url(); ?>assets/css/_common/images/loadmorespinner.gif" id="imageloadmore" />
            </h3>
        </div>
        <div style="float: right">
            <a href="#search_box">
                <img src="<?php echo base_url(); ?>assets/css/_common/images/search.jpg" title="Search" width="30px" height="30px">
            </a>
        </div>
    </header>

    <div id="tag_note" style="float: left; padding: 7px 5px 0 5px;"> Bạn đang tìm kiếm với từ khóa:  </div>
    <!--<input id="tag_list" type="hidden" name="tag_list" placeholder="Tags" class="tm-input tm-input-success" />-->
    <div id="tag_list"></div>
    <div style="clear: both"></div>
    <table class="tablesorter" cellspacing="0">
        <thead>
            <tr>
                <th>Ảnh đại diện</th>
                <th>Tên movie</th>
                <th>Diễn viên</th>
                <th>Tags</th>
                <th>Người tạo</th>
                <th>Cập nhật</th>
                <th>Ra mắt</th>
                <th>Trạng thái</th>
                <th>Xử lý</th>
            </tr>
        </thead>

        <tbody class="sortable" id="content" >
        </tbody>
    </table>

    <input id="end_timestamp" type="hidden" value="<?php echo time() ?>">    
    <input id="begin_index" type="hidden" value="0">    

    <div class="loadmore" id="loadmore">
        <button id="get_list_loadmore" title="Loadmore" onclick="loadmore(document.getElementById('tag_list').value)">
            Loadmore <img src="<?php echo base_url(); ?>assets/css/_common/images/loadmorespinner.gif" id="loadmorespinner" hidden="hidden"/>
        </button>
        
        <button id="full_search_loadmore" title="Loadmore" onclick="full_search_tag(document.getElementById('search_box').value, $('#begin_index').val()  )" hidden="hidden">
            Loadmore 
            <img src="<?php echo base_url(); ?>assets/css/_common/images/loadmorespinner.gif" id="loadmorespinner" hidden="hidden"/>
        </button>
    </div>

    <div style="" id="search_label">
        <input id="search_box" type="text" style="width: 100%; height: 1.5em;"
               placeholder="Nhập từ khóa để tìm kiếm" class="input-medium tm-input tm-input-success"/>    
        <input type="button" value="Search" onclick="full_search_tag(document.getElementById('search_box').value, 0)">
    </div>
</article> <!-- end of content manager article -->

</div>

<script>

    //Hiển thị danh sách các movie khi truy cập trang index
    $(document).ready(function() {
        $('#tag_note').hide();
        var timestamp= Math.round(new Date().getTime()/1000.0);
        $.ajax({
            url: $('#base_url').val() + "content/ajax_get_list_content",
            type: "POST",
            data: {
                end_timestamp: timestamp,
                contentType: 'movie'
            },
            dataType: 'json',
            success: function(msg) {
                var total_items = jQuery.parseJSON(msg.data).total_items;
                insert_movie_list(jQuery.parseJSON(msg.data), total_items, 'loadmore');
                $("#imageloadmore").hide();
            }
        });      
        
        jQuery("#search_box").typeahead({
        name: 'tags',
        limit: 15,
        prefetch: $('#base_url').val() + 'content/ajax_search_content?contentType=movie&type='+'&&keywords=%QUERY',
        remote: {
            url: $('#base_url').val() + 'content/ajax_search_content?contentType=movie&type='+'&&keywords=%QUERY',
            dataType: "json",
            timeout: 2000
        }
        }).on('typeahead:selected', function (e, d) {
              tagApi.tagsManager("pushTag", d.value);                        
        });
    });

    //Load more
    function loadmore(tag){
        $("#imageloadmore").show();
        $("#loadmorespinner").show();
        end_timestamp = $('#end_timestamp').val();
        $.ajax({            
            url: $('#base_url').val() + "content/ajax_get_list_content",
            type: "POST",
            data: {
                end_timestamp: end_timestamp, 
                arr_tag_label: tag,
                contentType: 'movie'
        },
            success: function (msg) {                
                insert_movie_list(jQuery.parseJSON(msg.data), '',  'loadmore');
                $("#imageloadmore").hide();
                $("#loadmorespinner").hide();
            }
        });
    }        


    //Lọc danh sách movie theo các tag
    function search_tag(tag){
        $(".myTag").remove();
        $("#imageloadmore").show();        
        $("div#tag_list").html("");
        
        if(tag != null){    
            $("div#tag_list").html('<input id="tagsManager" type="hidden" name="tag_list" class="tm-input tm-input-success tagsManager" />');        
            jQuery("#tagsManager").tagsManager({
                prefilled: [
                    tag
                ]
            });            
            
            $('#tag_note').show();
        }else{
            $(".myTag").remove();
            $('#tag_note').hide();
        }

        $('#tag_list').val(tag);
        end_timestamp = $('#end_timestamp').val();
        //        end_timestamp = $('#end_timestamp').val();
        $.ajax({
            url: $('#base_url').val() + "movie/ajax_get_list_movie",
            type: "POST",
            data: {arr_tag_label: tag},
            success: function (msg) {
                var total_items = jQuery.parseJSON(msg.data).total_items;
                insert_movie_list(jQuery.parseJSON(msg.data), total_items, 'search_tag');
                $("#imageloadmore").hide();
                $("#full_search_loadmore").hide();
                $("#get_list_loadmore").show(); 
            }
        });
    }
    
    //Search tất cả các movie có tag liên quan
    function full_search_tag(tag, begin_index){    
        $("#imageloadmore").show();  
        $("div#tag_list").html("");
        
        if(tag != null){    
            $("#tag_list").html(
                '<input id="tagsManager" type="hidden" id="tags_list" name="tag_list" class="tm-input tm-input-success tagsManager" />'
            );
            
            jQuery("#tagsManager").tagsManager({
                prefilled: [
                    tag
                ]          
            });            
            $('#tag_note').show();            
        }else{            
            $('#tag_note').hide();
        }        
        
        $.ajax({
            url: $('#base_url').val() + "content/ajax_full_search_content",
            type: "POST",
            data: {
                keyword: tag, 
                begin_index: begin_index,
                contentType: "movie"
            },
            success: function (msg) {
                if(begin_index == 0){
                    insert_movie_list(jQuery.parseJSON(msg.data), '', 'search_tag');
                }else{
                    insert_movie_list(jQuery.parseJSON(msg.data), '', 'loadmore');
                }
                
                $("#imageloadmore").hide();
                $("#full_search_loadmore").show();
                $("#get_list_loadmore").hide(); 
                $("#begin_index").val(parseInt($("#begin_index").val()) + jQuery.parseJSON(msg.data).movie.length);
            }
        });
    }
        
    
    function insert_movie_list(ArrayData, total_items, type){    
        var i; 
        var url = $('#base_url').val();    
        var jsonArray = ArrayData.content;                                
        if(total_items != '' && total_items != undefined){
            $("#total_movies").html("(" +total_items + ")" );
        }else{
            $("#total_movies").html("");
        } 
        
        
        if(jsonArray == null){
            data = '';
            data += '<tr>';
            data += '<td colspan="5">Không còn dữ liệu để hiển thị</td>';
            data += '</tr>';
            $("#imageloadmore").hide();
            $("#loadmore").hide();
        }
        else{
            data = '';                  
            for(i=0; i < jsonArray.length; i++){
                movie = jQuery.parseJSON(jsonArray[i][0]);
                console.log(movie);
                creator = jQuery.parseJSON(jsonArray[i][1]);
                end_timestamp = movie.update_timestamp;
                                        
                data += '<tr>';
                //Ảnh đai diện
                data += '<td>';
                data += '<img src="';
                if(movie.thumb_picture_uri != '') {
                    data += movie.thumb_picture_uri ;}
                else{
                    data += '/assets/css/_common/images/image.png" ';
                }
                data += ' "style="max-height:64px;max-width:64px"/> ';
                data += '</td>';

                //Tên movie
                data += '<td>' + movie.title + '</td>';

                //Diễn viên
                data += '<td>';
                if(movie.actors != null)
                    for (j = 0; j < (movie.actors.length); j++) {                            
                        if(movie.actors[j].nickname != '' )
                            data +=  '<a href="' + url +  'public_people/form/' + movie.actors[j].uid +' " id="actor" value="' + movie.actors[j].full_name
                            +')"> ' +  movie.actors[j].nickname + '</a>'                             
                    }                             
                data += '</td>';
                   
                //Tags
                data += '<td>';
                if(movie.tag_labels != null)
                    for (j = 0; j < (movie.tag_labels.length); j++) {
                        if(movie.tag_labels[j] != '')
                            data +=  '<a href="#" id="tag_label" value="' + movie.tag_labels[j]
                            + '"onclick="return search_tag('+"'"+movie.tag_labels[j]+"'"+')"> ' + "#" +  movie.tag_labels[j] + '</a>'
                    }
                data += '</td>';
                    
                //Người tạo
                data += '<td>';
                data += creator.full_name;
                data += '</td>';

                //Thời gian cập nhật
                data += '<td>';
                date = new Date(movie.update_timestamp * 1000 );
                datevalues = [date.getDate() + '/' + (date.getMonth()+1) + '/' + date.getFullYear() +'  ' + date.getHours() + ':' + date.getMinutes()];
                data += datevalues;
                data += '</td>';

                //thời gian công chiếu
                data += '<td>';
                date = new Date(movie.release_timestamp * 1000 );
                datevalues = [date.getDate() + '/' + (date.getMonth()+1) + '/' + date.getFullYear()];
                data += datevalues;
                data += '</td>';

                //Trạng thái
                data += '<td>';
                if(movie.hidden == true) {
                    data += 'Ẩn';
                }
                else {
                    data += 'Kích hoạt';
                };
                data += '</td>';

                //Xử lý
                data += '<td>';
                data += '<a href="form/' + movie.item_id + '">Lịch chiếu</a>';
                data += '</td>';

                data += '</tr>';
            }
            $('#end_timestamp').val(end_timestamp);                
        }   
        
        if(type=='loadmore'){
            $("#content").html($("#content").html()+data);
        }else{
            $("#content").html(data);
        }
    }
</script>